package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.autoFile;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.reactive.TransactionalOperator;
import org.springframework.transaction.reactive.TransactionalOperator;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImol implements DishService {

    @Autowired
    private DishMapper dishMapper;
   @Autowired
   private DishFlavorMapper dishFlavorMapper;
   @Autowired
   private SetmealDishMapper  setmealDishMapper;


    //根据id查询菜品
    @Override
    public List<Dish> GetById(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.getDishBycateGoryId(dish);


    }
    // 新增菜品


    @Transactional
    public void save(DishDTO dishDTO) {

        Dish dish=  Dish.builder()
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .description(dishDTO.getDescription())
                .status(StatusConstant.DISABLE)
                .build();
        dishMapper.saveDish(dish);
        Long id = dish.getId();
        log.info("新增菜品的口味{}",dishDTO.getFlavors().toString());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null&&flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.saveDishFlavor(flavors);
        }


    }

    // 菜品分页查询
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
//        Dish dish= new Dish();
//        BeanUtils.copyProperties(dishPageQueryDTO,dish);
        List<DishVO> dishList = dishMapper.pageQuery(dishPageQueryDTO);
        Page p= (Page<DishVO>) dishList;
        return new PageResult(p.getTotal(),p.getResult());

    }

    // 删除菜品
    @Override
    public void delete(List<Long> ids) {
        //判断菜品是否在起售中
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否关联套餐

          List<Long> list=  setmealDishMapper.getByDishId(ids);
          if(list!=null&&list.size()>0){
              throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
          }

        // 删除菜品基本数据
        for (Long id : ids) {
            dishMapper.delete(id);
            dishFlavorMapper.delete(id);
        }



    }

    @autoFile(value = OperationType.UPDATE)
    @Override
    public void startOrStop(Integer status,long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.update(dish);
    }

    // 修改菜品
    @Override
    public void updateDish(DishDTO dishDTO) {
        // 修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        Long id = dish.getId();

        // 修改口味
        // --> 先删除，后新增
        dishFlavorMapper.delete(dishDTO.getId());

        // 新增口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.saveDishFlavor(flavors);
        }




    }

    // 根据id查询菜品
    @Override
    public DishVO getById(Long id) {
      List<DishFlavor> l=  dishFlavorMapper.getByDishId(id);
        DishVO dishVO= dishMapper.queryById(id);
        dishVO.setFlavors(l);
        return dishVO;
    }

    /**
     * 根据分类id查询菜品
     * @param
     * @return
     */
//    @Override
//    public List<DishVO> GetDishByCategoryId(Long categoryId) {
//       List<DishVO> list= dishMapper.getBycateGoryId(categoryId);
//        for (DishVO d : list) {
//            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
//            d.setFlavors(flavors);
//        }
//
//        return list;
//
//    }

    /**
     * 根据分类id查询菜品-方法2
     * @param dish
     * @return
     */

    @Override
    public List<DishVO> GetDishByCategoryId1(Dish dish) {
       List<Dish> list= dishMapper.getDish(dish);
       List<DishVO> dishVOS=new ArrayList<>();
        for (Dish dish1 : list) {
           DishVO dishVO= new DishVO();
           BeanUtils.copyProperties(dish1,dishVO);
            List<DishFlavor> byDishId = dishFlavorMapper.getByDishId(dish1.getId());
            dishVO.setFlavors(byDishId);
            dishVOS.add(dishVO);
        }
        return dishVOS;

    }
}
